package com.litxuf.web.action.front;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.litxuf.FinalInformation;
import com.litxuf.logic.core.ManagerFactory;
import com.litxuf.logic.member.dao.model.School;
import com.litxuf.logic.member.manager.MemberManager;
import com.litxuf.util.StringTools;
import com.litxuf.util.mail.JavaMailSend;
import com.yeqiangwei.util.FormatDateTime;

public class MemberRegAction extends DispatchAction {
	Logger log = Logger.getLogger(this.getClass());

	/**
	 * 用户名唯一性校验 made in 2009-9-4下午04:01:56
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * 
	 */
	MemberManager manager = ManagerFactory.getFactory().getMemberManager();

	public ActionForward check(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		log.info("==>user=" + username);
		String result = "1";

		if (StringUtils.isBlank(username)) {
			result = "2";
			log.info("--> 用户名 不能为空");
		} else if (!StringTools.checkEmail(username)) {
			result = "3";
			log.info("--> Email格式错误");
		} else {

			School sc = manager.findMemberByName(username.toLowerCase());
			if (sc != null) {
				result = "1";
				log.info("==>user=" + username + " 重复不能注册");
			} else {
				result = "0";
				log.info("==>user=" + username + " 可以注册");
			}
		}

		response.setContentType("text/xml; charset=utf-8");
		response.getWriter().print(result);
		return null;
	}

	/**
	 * 注册 made in 2009-9-4下午04:01:44
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * 
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("memberEmail");
		String pwd = request.getParameter("memberpwd");
		String schoolname = request.getParameter("schoolname");

		if (StringUtils.isBlank(username)) {
			log.info("--> 用户名 不能为空");
			request.setAttribute("Error", "邮箱不能为空");
			return mapping.findForward("reg");
		} else if (!StringTools.checkEmail(username)) {
			log.info("--> Email格式错误");
			request.setAttribute("Error", "邮箱格式错误");
			return mapping.findForward("reg");
		}

		School user = new School();
		// 转化成小写
		user.setMemberName(username.toLowerCase());
		user.setMemberPwd(pwd);
		String infoTime = FormatDateTime.formatDateTime("yyyy-MM-dd HH:mm:ss");
		user.setRegdatetime(infoTime);
		user.setStatus("Y");
		user.setSchoolname(schoolname);
		user.setEmail(username.toLowerCase());

		String id = manager.saveMember(user);
		if (id == null) {
			log.info("--->注册失败 ");
			request.setAttribute("Error", "服务器异常 请稍候再注册");
			return mapping.findForward("reg");

		} else {

			 request.setAttribute("RESULT", "注册成功 请更新你的详细信息");
			// 1.写入session
			log.info("==>注册成功 用户id=" + id);
			request.getSession().setAttribute(FinalInformation.UserFrontLogin,
					user);
			// 2.发送欢迎邮件

			JavaMailSend send = ManagerFactory.getFactory().getJavaMailSend();

			try {
				send.sendTemplateMail(username, pwd, username);
				log.info("--> 发送欢迎邮件成功");
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			try {
				log.info(" -->开始跳转到用户中心首页");
				response.sendRedirect(request.getContextPath()
						+ "/member/index.jsp");

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
